package com.yunxi.service.dubboService.impl.vehicle;

import com.github.pagehelper.PageInfo;
import com.yunxi.model.vehicle.VehicleCert;
import com.yunxi.service.localService.vehicle.VehicleCertService;
import com.yunxi.vehicle.pojo.VehicleCertApproveVO;
import com.yunxi.vehicle.pojo.VehicleCertVO;
import com.yunxi.vehicle.service.VehicleCertDubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 合格证信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-05-22
 */
public class VehicleCertDubboServiceImpl implements VehicleCertDubboService {

    @Autowired
    @Qualifier(value = "vehicleCertServiceImpl")
    VehicleCertService vehicleCertService;


    @Override
    public VehicleCertVO getById(String id) throws InvocationTargetException, IllegalAccessException {
    VehicleCert vehicleCert = vehicleCertService.getById(id);
    VehicleCertVO vehicleCertVO = new VehicleCertVO();
        BeanUtils.copyProperties(vehicleCert,vehicleCertVO);
        return vehicleCertVO;
    }

    @Override
    public void saveOrUpdate(VehicleCertVO vehicleCertVO) throws InvocationTargetException, IllegalAccessException {
    VehicleCert vehicleCert = new VehicleCert();
        BeanUtils.copyProperties(vehicleCertVO,vehicleCert);
    vehicleCertService.saveOrUpdate(vehicleCert);
    }

    @Override
    public void removeById(String id) {
    vehicleCertService.removeById(id);
    }

    @Override
    public PageInfo<VehicleCertVO> findPageByVO(VehicleCertVO vehicleCertVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {

      PageInfo<VehicleCertVO> pageInfo = vehicleCertService.pageByVo(vehicleCertVO,pageNum,pageSize);

      return pageInfo;

   }


    @Override
    public VehicleCertVO getByVin(String vin) {
        return vehicleCertService.getByVin(vin);
    }

    @Override
    public void removeByList(List<VehicleCertVO> vehicleCertVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        vehicleCertService.removeByListVo(vehicleCertVOList);
    }

    /**
     * 合格证审核
     * @param vehicleCertApproveVO
     * @throws Exception
     */
    @Override
    public void approve(VehicleCertApproveVO vehicleCertApproveVO) throws Exception {
        VehicleCert vehicleCert = vehicleCertService.getById(vehicleCertApproveVO.getVehicleCertId());
        if(vehicleCert == null)throw new Exception("操作失败,无法根据主键:"+vehicleCertApproveVO.getVehicleCertId()+"找到对应记录!");
        Integer certStatus = vehicleCert.getCertStatus();
        if(certStatus != 2)throw new Exception("操作失败,只能审核财务审核状态的单据!");
        vehicleCert.setCertStatus(3);//待发运
        vehicleCert.setRelationId(vehicleCertApproveVO.getRelationId());
        vehicleCertService.saveOrUpdate(vehicleCert);
    }

    /**
     * 发运
     * @param vehicleCertApproveVO
     * @throws Exception
     */
    @Override
    public void transport(VehicleCertApproveVO vehicleCertApproveVO) throws Exception {
        VehicleCert vehicleCert = vehicleCertService.getById(vehicleCertApproveVO.getVehicleCertId());
        if(vehicleCert == null)throw new Exception("操作失败,无法根据主键:"+vehicleCertApproveVO.getVehicleCertId()+"找到对应记录!");
        Integer certStatus = vehicleCert.getCertStatus();
        if(certStatus != 3)throw new Exception("操作失败,只能发运待发运状态的单据!");
        vehicleCert.setCertStatus(4);//待发运
        vehicleCert.setTransportDate(new Date());
        vehicleCertService.saveOrUpdate(vehicleCert);
    }

    /**
     * 合格证管理查询分页
     * @param vehicleCertVO
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<VehicleCertVO> managePage(VehicleCertVO vehicleCertVO, Integer pageNum, Integer pageSize) throws Exception {
        return vehicleCertService.managePage(vehicleCertVO,pageNum,pageSize);
    }

}
